<?php

/**
 * This is the model class for table "goods_comment".
 *
 * The followings are the available columns in table 'goods_comment':
 * @property integer $id ID
 * @property string $short_title Short Title
 * @property string $title Title
 * @property string $thumb Thumb
 * @property string $create_time Create Time
 * @property integer $uid Uid
 * @property integer $parent_id Parent
 * @property string $update_time Update Time
 * @property integer $rate 评分
 * @property string $content 内容
 * @property integer $useful 有用
 * @property integer $unuseful Unuseful
 * @property string $object_type 针对评论
 * @property string $target_object_type Target Object Type
 * @property integer $target_object_id Target Object
 * @property string $nickname Nickname
 */
class GoodsComment extends ActiveRecord
{
	
	/**
	 * Returns the static model of the specified AR class.
	 * @return GoodsComment the static model class
	 */
	public static function model($className=__CLASS__) {
		return parent::model($className);
	}
	
	public function getMdcClass() {
		return 'application.models.metadata.GoodsCommentMetadata';
	}
	
	/**
	 * 获取被评论模型
	 * Enter description here ...
	 */
	public function getTargetObject() {
		$model = new $this->target_object_type;
		return $model->findByPk($this->target_object_id);
	}
	
	static public function radeArrayData($type, $id) {
		$count = 0;
		if (in_array($type, array('Hotel','Scenic'))) {
			$count = self::model()->count(self::model()->companyCriteria($type,$id));
		} else {
			$count = self::model()->countByAttributes(array('target_object_id' => $id,'target_object_type' => $type));
		}
		
		$goodRate = self::model()->countByRate($type, $id, 1);
		$centerRate = self::model()->countByRate($type, $id, 2);
		$lessRate = self::model()->countByRate($type, $id, 3);
		return array(
			'count' => $count,
			'goodRate' => $goodRate,
			'centerRate' => $centerRate,
			'lessRate' => $lessRate,
			'goodsRateModulus' => $count == 0 ? 0 : sprintf('%d',$goodRate/$count*100),
			'centerRateModulus' => $count == 0 ? 0 : sprintf('%d',$centerRate/$count*100),
			'lessRateModulus' => $count == 0 ? 0 : sprintf('%d',$lessRate/$count*100),
		);
	}
}